package com.sping_boot3.spirng_boot3.juc;

import lombok.SneakyThrows;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrier_Demo {
    static int number = 4;
    @SneakyThrows
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier =  new CyclicBarrier(number,() ->{
            System.out.println(Runtime.getRuntime().availableProcessors()+"--------------1");
        });
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            new Thread(() ->{
                System.out.println(finalI);
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            },String.valueOf(i)).start();
        }
        System.out.println("--------------");
    }
}
